### PACKAGE PREAMBLE ###

# Create a vector of package names
packages <- c("dplyr", "ggplot2", "tidyr", "tidyverse", "data.table", "readxl", "foreign", "devtools", "fixest", "sandwich", "modelsummary", "kableExtra")

installed_packages <- packages %in% rownames(installed.packages())
if (any(installed_packages == FALSE)) {
  install.packages(packages[!installed_packages])
}

# Packages loading
invisible(lapply(packages, library, character.only = TRUE))
rm(packages, installed_packages)

options(modelsummary_format_numeric_latex = "plain")
f <- function(x) format(x, digits = 3, nsmall = 2, scientific = FALSE)

getwd()
setwd(dir = "/Users/jmc4qg/The Lab Dropbox/Jonathan Colmer/ShotSpotter_env/Journal_submissions/ReStat/Replication Folder/")
mydata <- read.dta("Analysis Data/NIBRS_analysis.dta")

# Panel A: Baseline Temperature—Homicide Relationship involving a Firearm

T1_G <- feols(gun_homicide_pc~ tMean + prec , data=mydata)
summary(T1_G, cluster="fips")
T2_G <- feols(gun_homicide_pc~ tMean + prec | sample_month + week + dow, data=mydata)
summary(T2_G, cluster="fips")
T3_G <- feols(gun_homicide_pc~ tMean + prec | state_sample_month + week + dow, data=mydata)
summary(T3_G, cluster="fips")
T4_G <- feols(gun_homicide_pc~ tMean + prec | ori_sample_month + week + dow, data=mydata)
summary(T4_G, cluster="fips")

models <- list(T1_G, T2_G, T3_G, T4_G)
modelsummary(models, vcov = ~fips, output = "Figures and Tables/Table_C1/Table_C1_Panel_A.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))

# Panel B: Baseline Temperature—Homicide Relationship not involving a Firearm

T1_NG <- feols(ngun_homicide_pc~ tMean + prec , data=mydata)
summary(T1_NG)
T2_NG <- feols(ngun_homicide_pc~ tMean + prec | sample_month + week + dow, data=mydata)
summary(T2_NG, cluster="fips")
T3_NG <- feols(ngun_homicide_pc~ tMean + prec | state_sample_month + week + dow, data=mydata)
summary(T3_NG, cluster="fips")
T4_NG <- feols(ngun_homicide_pc~ tMean + prec | ori_sample_month + week + dow, data=mydata)
summary(T4_NG, cluster="fips")

models <- list(T1_NG, T2_NG, T3_NG, T4_NG)
modelsummary(models, vcov = ~fips, output = "Figures and Tables/Table_C1/Table_C1_Panel_B.tex", fmt = f, stars = c('*' = 0.1, '**' = 0.05, '***' = 0.01))

